package edu.uts.reekie.designer.actions;

import java.util.List;

import org.eclipse.gef.*;
import org.eclipse.gef.commands.*;
import org.eclipse.jface.resource.*;
import org.eclipse.ui.IWorkbenchPart;
import edu.uts.reekie.designer.*;
import edu.uts.reekie.designer.edit.*;
import edu.uts.reekie.designer.model.*;

public abstract class ConnectorLabelAction extends org.eclipse.gef.ui.actions.SelectionAction {


	protected abstract String generateId();

	Request request;
	
	public ConnectorLabelAction(IWorkbenchPart part) {
		super(part);
		// TODO: use messages instead of constants
		setText(generateId());
		setToolTipText(generateId());
		setId(generateId());
		
		// TODO: change to real icon
		setImageDescriptor(
				ImageDescriptor.createFromFile(ReekieDesignerPlugin.class,"icons/plus.gif")); //$NON-NLS-1$
		
		
		setHoverImageDescriptor(getImageDescriptor());
		

		request = new Request(generateId());
		
	}
	
	protected boolean calculateEnabled() {
		return canPerformAction();
	}


	private boolean canPerformAction() {
		
		if (getSelectedObjects().isEmpty())
			return false;
		
		List<?> parts = getSelectedObjects();
		for (int i=0; i<parts.size(); i++){
			Object o = parts.get(i);
			if (!(o instanceof AbstractConnectorEditPart))
				return false;
			
			AbstractConnectorEditPart part = (AbstractConnectorEditPart)o;
			if (!(part.getModel() instanceof AbstractConnector))
				return false;
			
			
			if (!canPerformAction(part)) {
				return false;
			}
			
		}
		return true;
	}
	
	protected boolean canPerformAction(AbstractConnectorEditPart part) {
		AbstractConnector model = (AbstractConnector)part.getModel();
		return canPerformAction(model);
	}
	
	protected abstract boolean canPerformAction(AbstractConnector connector);

	
	protected abstract Command getCommand();
	
	/*
	private Command getCommand() {
		
		List editparts = getSelectedObjects();
		CompoundCommand cc = new CompoundCommand();
		cc.setDebugLabel("Set Connector Direction");//$NON-NLS-1$
		for (int i=0; i < editparts.size(); i++) {
			EditPart part = (EditPart)editparts.get(i);
			cc.add(part.getCommand(request));
		}
		return cc;
	}
	*/

	public void run() {
		execute(getCommand());
	}

}
